package com.fwtai.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.datasource.DaoHandle;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * 合伙|代理的特殊区域访问数据库
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2023-07-22 14:36:48
 * @QQ号码 444141300
 * @Email service@yinlz.com
 * @官网 <url>http://www.fwtai.com</url>
*/
@Repository
public class PartnerAreaDao{

  private final DaoHandle dao;

  public PartnerAreaDao(final DaoHandle dao){
    this.dao = dao;
  }

  public int add(final PageFormData formData){
    return dao.execute("partner_area.add",formData);
  }

  public int edit(final HashMap<String,Object> map){
    return dao.execute("partner_area.edit",map);
  }

  public Long queryExistById(final long area_id){
    return dao.queryForLong("partner_area.queryExistById",area_id);
  }

  public HashMap<String,Object> queryById(final long area_id){
    return dao.queryForHashMap("partner_area.queryById",area_id);
  }

  @Transactional
  public int editEnableKid(final HashMap<String,Object> map){
    dao.execute("partner_area.editPartnerEnable",map);
    return dao.execute("partner_area.deletedById",map);
  }

  public int editEnable(final HashMap<String,Object> map){
    return dao.execute("partner_area.deletedById",map);
  }

  @Transactional
  public int editPartnerKid(final HashMap<String,Object> map){
    dao.execute("partner_area.editPartnerDisable",map);
    return dao.execute("partner_area.deletedById",map);
  }

  public int delByKeys(final long[] list){
    return dao.execute("partner_area.delByKeys",list);
  }

  public List<HashMap<String,Object>> getListData(final PageFormData formData){
    return dao.queryForListHashMap("partner_area.listData",formData);
  }

  public Integer getListTotal(final PageFormData formData){
    return dao.queryForInteger("partner_area.listTotal",formData);
  }

  public List<HashMap<String,Object>> getUserList(final String word){
    return dao.queryForListHashMap("partner_area.get_user_list",word);
  }

  public boolean getExistProxy(final Long userId){
    return dao.queryForListHashMap("partner_area.getExistProxy",userId) .size() > 0;
  }

  public HashMap<String,Object> getAreaExist(final HashMap<String,Object> map){
    return dao.queryForHashMap("partner_area.getAreaExist",map);
  }

  @Transactional
  public int updateProxy(final PageFormData formData){
    dao.execute("partner.delPartnerRole",formData);//把之前已加入的角色移除角色
    dao.execute("partner_area.add_partner_role",formData);//把新的userId加入角色信息,可重复插入,uid

    dao.execute("partner_area.deleteInviter",formData);//置空邀请人
    dao.execute("partner_area.editInviter",formData);//手动绑定推荐人|更换推广人,uid

    return dao.execute("partner_area.update_proxy",formData);
  }

  @Transactional
  public int addProxy(final PageFormData formData){
    dao.execute("partner.addPartnerRole",formData);//可重复插入
    dao.execute("partner_area.updateInviter",formData);//手动绑定推荐人|更换推广人
    return dao.execute("partner_area.addProxy",formData);
  }

  public int editProxyInfo(final PageFormData formData){
    return dao.execute("partner_area.editProxy",formData);//仅修改资料
  }

  //不是同一个人,①把之前已加入的角色移除角色;把新的userId加入角色信息;②把之前的‘绑定推荐人’置空,新的userId加入新的推广人
  @Transactional
  public int editProxy(final PageFormData formData){
    dao.execute("partner.delPartnerRole",formData);//把之前的用户已加入的角色移除角色
    dao.execute("partner_area.add_partner_role",formData);//把新的userId加入角色信息,可重复插入,uid
    dao.execute("partner_area.deleteInviter",formData);//置空之前的代理的邀请人
    dao.execute("partner_area.editInviter",formData);//手动绑定推荐人|更换推广人,uid
    dao.execute("partner_area.editProxy",formData);
    return dao.execute("partner_area.addNewProxy",formData);
  }

  public Long getPartnerUserId(final Long kid){
    return dao.queryForLong("partner_area.getPartnerUserId",kid);
  }
}